using Ant.Dto.PersonnelDto;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using System.Collections.Generic;
using Ant.EntityFrameworkCore.Model;
using Ant.DbServiceInterface;
using Microsoft.AspNetCore.Authorization;

namespace Ant.Service.PersonnelManagement.Conntroller
{
    [Route("ant/{controller}/{action}")]
    public class UserController : Controller
    {
        private IPersonneDataService _dbService;
        private IMapper _mapper;
        public UserController(IPersonneDataService dbService, IMapper mapper)
        {
            _dbService = dbService;
            _mapper = mapper;
        }


        [HttpGet]
        public IActionResult Index()
        {
            return Ok("test");
        }
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> GetUser()
        {
            var test = await _dbService.GetUserAsync();
            if (test == null) return BadRequest("无数据");

            var temp = _mapper.Map<IEnumerable<GetUserResDto>>(test);
            return Ok(temp);
        }


        [HttpPost]
        public IActionResult AddUser([FromBody] AddUserDto userDto)
        {
            var temp = _mapper.Map<UserInfo>(userDto);
            if (_dbService.AddUser(temp))
                return Ok();
            else
                return BadRequest();
        }

        [HttpDelete]
        [Authorize(Roles = "Admin")]
        public IActionResult DelUser([FromQuery] int ID)
        {
            if (_dbService.DelUser(ID))
                return Ok("删除成功");
            else
                return BadRequest("删除失败");

        }

        [HttpPut]
        public IActionResult ExitStore([FromBody] UpdateJoinStoreDto dto){
            if (_dbService.ExitStore( dto.storeID,dto.userID))
            {
                return Ok("退出成功");
            }else{
                return BadRequest("退出失败");
            }
        }

        [HttpPut]
        public IActionResult UpdateJoinStore([FromBody] UpdateJoinStoreDto dto )
        {
            if (_dbService.UpdateJoinStore( dto.storeID,dto.userID))
            {
                return Ok("加入成功");
            }else{
                return BadRequest("加入失败");
            }
        }
    }


}